<template>
  <q-page class="cc-admin">
    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <div class="row">
        <q-item class="col-md-3 col-sm-6 col-xs-12 items-center">
          <q-item-label>岗位名称：</q-item-label>
          <q-item-section class="col row">
            <q-input outlined dense v-model="searchForm.postName" />
          </q-item-section>
        </q-item>
        <q-item class="col-md-3 col-sm-6 col-xs-12 items-center">
          <q-item-section>
            <div class="row justify-center">
              <q-btn
                unelevated
                no-wrap
                label="查询"
                color="primary"
                class="q-mx-xs"
                icon="search"
                :loading="loading"
                @click="query()"
              >
                <template v-slot:loading>
                  <q-spinner-ios class="on-center" />
                </template>
              </q-btn>
              <q-btn
                outline
                no-wrap
                unelevated
                label="重置"
                class="q-mx-xs"
                color="primary"
                icon="refresh"
                @click="searchReset"
              />
            </div>
          </q-item-section>
        </q-item>
      </div>
      <q-table
        flat
        color="primary"
        class="cross_table"
        separator="cell"
        :columns="columns"
        :data="list"
        row-key="id"
        :pagination.sync="pagination"
        :visible-columns="group"
        @request="query"
        :rows-per-page-options="[10, 20, 50, 100]"
        :selection="mobile?'none':'multiple'"
        :selected.sync="selected"
      >
        <template v-slot:top="table">
          <div class="row no-wrap full-width">
            <q-space />
            <table-top-btns label="岗位管理" modelName="position" :showAdd="true"
            :table="table" :selected="selected" :importing="importing" :exporting="exporting"
            :importExcelUrlFull="importExcelUrlFull" :columns="columns" @groupChange="groupChange"
            @add="add" @importExcel="importExcel" @exportExcel="exportExcel" @showConfirm="showConfirm"
            :showImport="false" :showExport="false"/>
          </div>
        </template>
        <template #body-cell-type="props">
          <q-td key="type" :props="props">
            {{ getDictLabel(positionType, props.row.type) }}
          </q-td>
        </template>
        <template #body-cell-departId="props">
          <q-td key="departId" :props="props">
            {{ getDictLabel(sysDepart, props.row.departId) }}
          </q-td>
        </template>
        <template #body-cell-isMinister="props">
          <q-td key="isMinister" :props="props">
            {{ getDictLabel(yesNo, props.row.isMinister) }}
          </q-td>
        </template>
        <template #body-cell-isOfficers="props">
          <q-td key="isOfficers" :props="props">
            {{ getDictLabel(yesNo, props.row.isOfficers) }}
          </q-td>
        </template>
        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
          <cc-btn label="编辑" color="primary" icon="edit" @click="edit(props.row)"> <q-tooltip>编辑</q-tooltip></cc-btn>
            <cc-btn label="复制" color="primary" icon="mdi-content-copy" @click="copy(props.row)"> <q-tooltip>复制</q-tooltip></cc-btn>
            <btn-del label="岗位管理" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>
    <cc-dialog ref="dialog" :title="`${editType}岗位管理`" @submit="submit">

            <div class="col-6">
              <h5><q-icon name="star" color="red" />岗位名称：</h5>
              <q-input outlined dense v-model="form.postName" type="text" :rules="[requiredTest]"/>
            </div>
            <div class="col-6">
              <h5><q-icon name="star" color="red" />岗位编码：</h5>
              <q-input outlined dense v-model="form.postCode" type="text" :rules="[requiredTest]" />
            </div>
            <div class="col-6">
              <h5>类型：</h5>
              <q-select
                outlined
                dense
                emit-value
                v-model="form.type"
                map-options
                :options="positionType"
              />
            </div>
            <div class="col-6">
              <h5><q-icon name="star" color="red" />所属部门：</h5>
              <select-search
                outlined
                dense
                emit-value
                v-model="form.departId"
                map-options
                :options="sysDepart"
                :rules="[requiredTest]"
                @input="sysDeptCodeInput(form.sysDeptCode,form)"
              />
            </div>
            <div class="col-6">
              <h5>是否为部门长：</h5>
              <q-select
                outlined
                dense
                emit-value
                v-model="form.isMinister"
                map-options
                :options="yesNo"
              />
            </div>
            <div class="col-6">
              <h5>是否为高级船员：</h5>
              <q-select
                outlined
                dense
                emit-value
                v-model="form.isOfficers"
                map-options
                :options="yesNo"
              />
            </div>
            <div class="col-6">
              <h5><q-icon name="star" color="red" />排序：</h5>
              <q-input outlined dense v-model="form.px" type="text" :rules="[requiredTest]" />
            </div>
          </cc-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
    <cc-loading :loading="loading"/>
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import { requiredTest, phoneTest } from 'boot/inputTest';

export default {
  name: 'SysPosition',
  mixins: [IndexMixin],
  components: {
    confirm,
  },
  data() {
    return {
      columns: [
        {
          name: 'index',
          align: 'center',
          label: '序号',
          field: 'index',
          headerClasses: 'cc-table-index',
        },
        {
          name: 'postName', align: 'center', label: '岗位名称', field: 'postName',
        },
        {
          name: 'postCode', align: 'center', label: '岗位编码', field: 'postCode',
        },
        {
          name: 'type', align: 'center', label: '类型', field: 'type',
        },
        {
          name: 'departId', align: 'center', label: '所属部门', field: 'departId',
        },
        {
          name: 'isMinister', align: 'center', label: '是否为部门长', field: 'isMinister',
        },
        {
          name: 'isOfficers', align: 'center', label: '是否为高级船员', field: 'isOfficers',
        },
        {
          name: 'px', align: 'center', label: '排序', field: 'px',
        },
        {
          name: 'opt', align: 'center', label: '操作', field: 'id',
        },
      ],
      positionType: [{ value: '1', label: '岸基' }, { value: '2', label: '船舶' }, { value: '3', label: '外委单位' }],
      sysDepart: [],
      yesNo: [{ value: '1', label: '是' }, { value: '0', label: '否' }],
      showQuery: true,
      headers: [{ name: 'Authorization', value: localStorage.Authorization }],
      uploadUrl: `${process.env.BASE_URL}/sys/common/upload`,
      imgUrl: `${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/sys/position/list',
        add: '/sys/position/add',
        edit: '/sys/position/edit',
        copy: '/sys/position/copy',
        delete: '/sys/position/delete',
        deleteBatch: '/sys/position/deleteBatch',
        exportXlsUrl: '/sys/position/exportXls',
        importExcelUrl: '/sys/position/importExcel',
      },
    };
  },
  methods: {
    requiredTest,
    getDictLabel,
    initDict() {
      this.$axios.get('/sys/depart/getSysOrgCode?deptType=dept').then((r) => {
        this.sysDepart = r;
      });
    },
    sysDeptCodeInput(v, row, editAfter) {
      // 这里给名称字段赋值
      if (v instanceof Array) {
        const nameArray = [];
        this.form.sysDepartName = '';
        v.forEach((id) => {
          const opt = this.sysDepart.find((o) => o.value === id);
          if (opt) {
            nameArray.push(opt.label);
          }
          this.form.sysDepartName = nameArray.join(',');
        });
      } else {
        const opt = this.sysDepart.find((o) => o.value === v);
        this.form.sysDepartName = opt ? opt.label : '';
      }
    },
    submit() {
      let q;
      if (this.editType === '新建' || this.editType === '复制') {
        q = this.$axios.post(this.url.add, this.form);
      } else {
        q = this.$axios.put(this.url.edit, this.form);
      }
      return q.then((r) => {
        this.$info(r.message);
        this.$refs.dialog.hide();
      }).finally(() => {
        this.query();
      });
    },
    copy(row) {
      this.editType = '复制';
      this.form = { ...row, id: null };
      this.$refs.dialog.show();
    },

    updateCatalog() {
      console.log('sss1');
    },
  },
  mounted() {
    this.initDict();
  },

};
</script>

<style lang="stylus"></style>
